package com.zc.mall;

import com.zc.mall.consts.MallConst;
import com.zc.mall.exception.UserLoginExcepiton;
import com.zc.mall.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName UserLoginInterceptor.java
 * @Description 拦截器
 * @createTime 2021年06月05日 11:33:00
 */
@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {

    /**
     * true 表明继续执行
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进入 preHandle --------------------");
        User user = (User)request.getSession().getAttribute(MallConst.CURRENT_USER);
        if ( user == null ) {
            //第一种方法：response.getWriter().println();//然后再json序列化
            throw new UserLoginExcepiton();
//            return false;
//            return ResponseVo.error(NEED_LOGIN);
        }
        return true;
    }

}
